import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store/store'
const Home = () => import('@/pages/Home.vue')
const userinfo = ()=>import('@/pages/userinfo.vue')
const edit = ()=>import('@/pages/Edit.vue')
const article = ()=>import('@/pages/Article.vue')
const dashboard = ()=> import('@/pages/dashboard.vue')
const search = ()=> import('@/pages/search.vue')



Vue.use(VueRouter)

const router = new VueRouter({
  routes :  [
    {
      path:'/',
      component:Home,
      meta:{
        keepalive:true
      }
    },
    {
      path : '/userinfo' ,
      component:userinfo ,
      meta:{
        istoken:true ,
        keepalive:true
      }
    } ,
    {
      path:'/edit',
      component:edit,
      meta:{
       istoken:true
     }
    },
    {
      path:'/vedio/:id',
      component:article
    },

   {
    path:'/dashboard',
    component:dashboard,
    meta:{
      Admin:true
    }
  } ,
  {
    path:'/search' ,
    component:search
  } ,
  ] ,
  mode:'history'
})


router.beforeEach((to,from,next) => {
  if(to.meta.Admin&& !store.getters.isAdmin){
    Vue.prototype.$router.push("/")
    return ;
  }
  if(!store.getters.isLogin &&to.meta.istoken == true){
    Vue.prototype.$msg.fail('请先登录')
    return
  }
  next()
})
export default router
